package com.lzx.iteam;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.*;
import com.lzx.iteam.R;
import com.lzx.iteam.adapter.ChooseCloudGroupAdapter;
import com.lzx.iteam.bean.ApprovalData;
import com.lzx.iteam.bean.AttendanceData;
import com.lzx.iteam.bean.ChatContact;
import com.lzx.iteam.bean.CloudGroup;
import com.lzx.iteam.contactssearch.ContactsDBReader;
import com.lzx.iteam.net.CloudGroupListCenter;
import com.lzx.iteam.util.Constants;
import com.lzx.iteam.util.PreferenceUtil;
import com.lzx.iteam.util.StringUtil;
import com.lzx.iteam.widget.MyListView;
import io.rong.imageloader.core.ImageLoader;

import java.util.ArrayList;

import static android.R.attr.targetId;

/**
 * Created by Megan on 15/6/26.
 */
public class ChooseContactByTeamActivity extends Activity implements View.OnClickListener {
    private TextView mCancle, mTitle;
    private MyListView mGroupCloudlist;
    private TextView mTvMyContacts;
    private ChooseCloudGroupAdapter mAdapter;
    private CloudGroupListCenter mCenter;
    private ArrayList<CloudGroup> mGroupListData;
    private String mAction;
    private Bundle mBundle;

    String mChatGroupId;
    boolean mIsSingle;
    private ArrayList<ChatContact> mChatContacts;

    protected ImageLoader imageLoader = ImageLoader.getInstance();

    //事件信息
    String eventTypeId, eventId, eventTmpId;
    String eventTypeName, eventReceiver, eventTheme;
    String eventPart1, eventPart2, eventPart3;
    String eventHint1, eventHint2, eventHint3;

    private AttendanceData attendanceData;
    private ApprovalData approvalData;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.choose_contact_by_team);
        initViews();
        initIntentDatas();
        setCloudListData();
        SealAppContext.getInstance().pushActivity(this);

    }

    @Override
    protected void onResume() {
        super.onResume();
        //mTvMyContacts.setText(ContactsDBReader.ALL_CONTACTS_NUMBER + "个联系人");
    }

    private void initViews() {
        mTitle = (TextView) findViewById(R.id.tv_create_group_name);
        mCancle = (TextView) findViewById(R.id.tv_create_group_cancle);
        mGroupCloudlist = (MyListView) findViewById(R.id.group_cloud_list);
        mCancle.setOnClickListener(this);
        //mGroupCloudlist.addHeaderView(headView(LayoutInflater.from(this)));
        mGroupCloudlist.setOnItemClickListener(new MyGroupOnItemClickListener());
        mCenter = CloudGroupListCenter.getInstance(this);
        mAdapter = new ChooseCloudGroupAdapter(this, imageLoader);
    }

    private void initIntentDatas() {
        Intent intent = getIntent();
        mAction = intent.getAction();
        if ("create_chat_group".equals(mAction)) {//发起群聊选择号码簿
            mChatGroupId = intent.getStringExtra("chat_group_id");
            mChatContacts=(ArrayList<ChatContact>)intent.getSerializableExtra("member");
            mIsSingle=intent.getBooleanExtra("isSingle",true);
            if(StringUtil.isEmpty(mChatGroupId)){
                mTitle.setText("发起群聊");
            }else{
                mTitle.setText("添加群聊成员");
            }

        } else if ("events_add_receiver".equals(mAction)) {
            mTitle.setText("选择收件人");
            mBundle = intent.getBundleExtra("event_msg");
        } else if ("event_add_receiver".equals(mAction)) {
            mTitle.setText("添加事件收件人");
            eventId = intent.getStringExtra("event_id");
            eventTypeId = intent.getStringExtra("event_type");
        } else if ("schedule_manager_add".equals(mAction)) {
            mTitle.setText("添加日程管理员");
        } else if ("attendance_member_add".equals(mAction)) {
            attendanceData=(AttendanceData)intent.getSerializableExtra("attendanceData");
            mTitle.setText("添加考勤成员");
        } else if ("approval_leader_add".equals(mAction)){
            approvalData = (ApprovalData) intent.getSerializableExtra("approvalData");
            mTitle.setText("添加审批人");
        }
    }

    public View headView(LayoutInflater inflater) {
        View view = inflater.inflate(R.layout.choose_cloud_group_item, null);
        mTvMyContacts = (TextView) view.findViewById(R.id.group_choose_use_msg);
        view.setVisibility(View.GONE);
        return view;
    }


    /**
     * 创建小组 添加小组成员
     * 创建事件 添加收件人
     */
    class MyGroupOnItemClickListener implements AdapterView.OnItemClickListener {

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
           /*
            if (position == 0) {
                if (ContactsDBReader.ALL_CONTACTS_NUMBER == 0 || ContactsDBReader.mGroupList.isEmpty() || ContactsDBReader.mNoGroupsSet.isEmpty()) {
                    ContactsDBReader.resetAllContactsNumber(ChooseContactByTeamActivity.this);
                }
                if ("create_chat_group".equals(mAction)) {//发起群聊选择号码簿
                    addChatGroupMember();
                } else if ("events_add_receiver".equals(mAction)) {
                    addEventReceiver();
                }else if("event_add_receiver".equals(mAction)){
                    Intent intent = new Intent(ChooseContactByTeamActivity.this, CloudDialerActivity.class);
                    intent.setAction(mAction);
                    intent.putExtra("event_id",eventId);
                    intent.putExtra("event_type",eventTypeId);
                    intent.putExtra("where","local");
                    startActivity(intent);
                }
            } else {*/
            if (Constants.isFastClick()) {
                return;
            }
            position++;
            if (mGroupListData != null && mGroupListData.size() > 0) {
                Adapter adapter = parent.getAdapter();
                CloudGroup item = (CloudGroup) adapter.getItem(position - 1);
                CloudDialerActivity.selectedGroup = item;
                CloudDialerActivity.selectedGroup.position = position;
                Intent intent = new Intent(ChooseContactByTeamActivity.this, CloudDialerActivity.class);
                intent.setAction(mAction);
                intent.putExtra("group_id", mGroupListData.get(position - 1).groupId + "");
                intent.putExtra("tag_id", "0");
                intent.putExtra("tag_name", mGroupListData.get(position - 1).groupName);
                intent.putExtra("group_name", mGroupListData.get(position - 1).groupName);
                intent.putExtra("group_img", mGroupListData.get(position - 1).groupImg);
                intent.putExtra("group_type", mGroupListData.get(position - 1).groupType);
                intent.putExtra("user_type", mGroupListData.get(position - 1).userType);
                intent.putExtra("contact_count", mGroupListData.get(position - 1).contactCount);
                intent.putExtra("contact_id", mGroupListData.get(position - 1).contactId);
                intent.putExtra("where", "cloud");
                if ("create_chat_group".equals(mAction)) {//发起群聊选择号码簿
                    intent.putExtra("chat_group_id", mChatGroupId);
                    intent.putExtra("member",mChatContacts );
                    intent.putExtra("isSingle",mIsSingle);
                } else if ("events_add_receiver".equals(mAction)) {
                    mBundle.putString("where", "cloud");
                    intent.putExtra("event_msg", mBundle);
                } else if ("event_add_receiver".equals(mAction)) {
                    intent.setAction(mAction);
                    intent.putExtra("event_id", eventId);
                    intent.putExtra("event_type", eventTypeId);
                    intent.putExtra("where", "cloud");
                } else if ("schedule_manager_add".equals(mAction)) {
                } else if ("attendance_member_add".equals(mAction)) {
                    intent.putExtra("attendanceData",attendanceData);
                } else if ("approval_leader_add".equals(mAction)){
                    intent.putExtra("approvalData",approvalData);
                }
                startActivity(intent);
            }
        }
        // }
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tv_create_group_cancle:
                CloudDialerActivity.mBatchOperationType = 0;
                finish();
                break;
        }
    }

    /**
     * 设置号码群群列表数据
     */
    private void setCloudListData() {
        String uid = PreferenceUtil.getInstance(this).getString(PreferenceUtil.CLIENT_USEID,"-1");
        mGroupListData = mCenter.getCloudGroupList(this,uid);
        mCenter.setGroupListListener(new CloudGroupListCenter.CloudGroupDataListener() {
            @Override
            public void onChange(ArrayList<CloudGroup> groupList) {
                mGroupListData.clear();
                mGroupListData.addAll(groupList);
                mAdapter.updateCloudGroupList(groupList);
                mAdapter.notifyDataSetChanged();
            }

            @Override
            public void onNoChange(ArrayList<CloudGroup> groupList) {
                mGroupListData.clear();
                mGroupListData.addAll(groupList);
                mAdapter.updateCloudGroupList(mGroupListData);
                mAdapter.notifyDataSetChanged();
            }

            @Override
            public void onError(int errNum, String obj) {
                Toast.makeText(ChooseContactByTeamActivity.this, obj, Toast.LENGTH_LONG).show();
            }
        });
        mAdapter.updateCloudGroupList(mGroupListData);
        mGroupCloudlist.setAdapter(mAdapter);
    }

    //添加小组成员-在本地选择
    public void addChatGroupMember() {
        Intent intent = new Intent(ChooseContactByTeamActivity.this, CloudDialerActivity.class);
        intent.setAction(mAction);
        intent.putExtra("where", "local");
        intent.putExtra("chat_group_id", mChatGroupId);
        startActivity(intent);
    }

    //事件收件人-在本地选择
    public void addEventReceiver() {
        Intent intent = new Intent(ChooseContactByTeamActivity.this, CloudDialerActivity.class);
        intent.setAction(mAction);
        mBundle.putString("where", "local");
        intent.putExtra("event_msg", mBundle);
        startActivity(intent);
    }
}
